function isYYYY(date) {
    new Date(date)
}
/**
 * @desc 返回参数的数据类型
 * @param field {any}
 * @returns {'string'|'number'|'boolean'|'undefined'|'null'|'object'|'function'|'array'|'date'|'regexp'}
 */
function fieldType (field) {
    return Object.prototype.toString.call(field).slice(8, -1).toLowerCase()
}

let types = ['string', 'number', 'array']
/**
 * 接口参数校验
 * @param fields {object}
 * @param rules {Array<{key:string,rule:Array}>}
 * - key: {string} 字段
 * - rule: {Array} 规则
 *      - type: {'string' | 'number' | 'array'} 类型(字符串|数字|数组)
 *      - error!: {string} 错误信息
 *      - required: {boolean} 是否必需
 *      - pattern: {Regexp} 正则表达式
 */
function verifyParams (fields, rules) {
    let error = undefined
    for (const item of rules) {
        let key = item.key
        let rule = item.rule
        for (const it of rule) {

            // verify required
            if (it.required && fieldType(fields[key]) === 'undefined') {
                error = it.error || `${key}: type is required!`
                break
            }
            // verify type
            if (it.type) {
                if (!types.includes(it.type)) {
                    error = `${key} type: ${it.type} is not supported!`
                    break
                }
                if (fieldType(fields[key]) !== it.type) {
                    error = it.error || `${key}: type is not a ${fieldType(fields[key])}!`
                    break
                }
            }
            // verify regexp
            if (it.pattern) {
                if (!(new RegExp(it.pattern).test(fields[key]))) {
                    error = it.error || `${key}: type is not a ${fieldType(fields[key])}!`
                    break
                }
            }
        }
    }
    return error
}

/**
 * websocket 返回信息
 * @param ws
 * @param url
 * @param data
 */
function wsSend(ws, url, data) {
    ws.send(JSON.stringify({
        url,
        data
    }))
}
module.exports = {
    verifyParams,
    wsSend
}



